<template>
  <div class="table-page">
    <GiTable
      row-key="id"
      title="我的简历"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #custom-left>
	        <a-input v-model="queryForm.name" placeholder="请输入简历名称" allow-clear @change="search">
	          <template #prefix><icon-search /></template>
	        </a-input>
        <a-button @click="reset">重置</a-button>
      </template>
      <template #custom-right>
        <a-upload :show-file-list="false" :custom-request="handleUpload">
          <template #upload-button>
            <a-button type="primary" shape="round">
              <template #icon>
                <icon-upload />
              </template>
              <template #default>上传简历</template>
            </a-button>
          </template>
        </a-upload>
      </template>

      <template #name="{ record }">
        <a-link @click="onDetail(record)">{{ record.name }}</a-link>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link @click="onUpdate(record)">修改</a-link>
          <a-link
            status="danger"
            :disabled="record.disabled"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <ResumeAddModal ref="ResumeAddModalRef" @save-success="search" />
    <ResumeDetailDrawer ref="ResumeDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import ResumeAddModal from './ResumeAddModal.vue'
import ResumeDetailDrawer from './ResumeDetailDrawer.vue'
import {type ResumeResp, type ResumeQuery, deleteResume, listResume, addResume} from '@/apis'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import {Message, type RequestOption} from "@arco-design/web-vue";

defineOptions({ name: 'Resume' })

const queryForm = reactive<ResumeQuery>({
  studentId: "",
  name: "",
  sort: ['createTime,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listResume({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  { title: '简历名称', dataIndex: 'name', slotName: 'name' },
  { title: '创建时间', dataIndex: 'createTime', slotName: 'createTime' },
  {
    title: '操作',
    slotName: 'action',
    width: 130,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
  }
]

// 上传简历
const handleUpload = (options: RequestOption) => {
  const controller = new AbortController()
  ; (async function requestWrap() {
    const { onProgress, onError, onSuccess, fileItem, name = 'file' } = options
    onProgress(20)
    const formData = new FormData()
    formData.append(name as string, fileItem.file as Blob)
    try {
      const res = await addResume(formData)
      Message.success('上传成功')
      onSuccess(res)
      search()
    } catch (error) {
      onError(error)
    }
  })()
  return {
    abort() {
      controller.abort()
    }
  }
}

// 重置
const reset = () => {
  queryForm.name = ""
  search()
}

// 删除
const onDelete = (item: ResumeResp) => {
  return handleDelete(() => deleteResume(item.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}


const ResumeAddModalRef = ref<InstanceType<typeof ResumeAddModal>>()
// 新增
const onAdd = () => {
  ResumeAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (item: ResumeResp) => {
  ResumeAddModalRef.value?.onUpdate(item.id)
}

const ResumeDetailDrawerRef = ref<InstanceType<typeof ResumeDetailDrawer>>()
// 详情
const onDetail = (item: ResumeResp) => {
  ResumeDetailDrawerRef.value?.onDetail(item.id)
}
</script>

<style lang="scss" scoped></style>
